package com.bdmc.web.controller.base;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.base.domain.vo.BaseIconVo;
import com.bdmc.base.domain.bo.BaseIconBo;
import com.bdmc.base.service.IBaseIconService;
import com.bdmc.common.core.page.TableDataInfo;


/**
 * 资源图标控制器
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/icon")
public class BaseIconController extends BaseController {

    private final IBaseIconService iBaseIconService;

    /**
     * 查询资源图标列表
     */
    @SaCheckPermission("base:icon:list")
    @GetMapping("/list")
    public TableDataInfo<BaseIconVo> list(@Validated(QueryGroup.class) BaseIconBo bo, PageQuery pageQuery) {
        return iBaseIconService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出资源图标列表
     */
    @SaCheckPermission("base:icon:export")
    @Log(title = "资源图标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BaseIconBo bo, HttpServletResponse response) {
        List<BaseIconVo> list = iBaseIconService.queryList(bo);
        ExcelUtil.exportExcel(list, "资源图标", BaseIconVo.class, response);
    }

    /**
     * 获取资源图标详细信息
     */
    @SaCheckPermission("base:icon:query")
    @GetMapping("/{iconId}")
    public AjaxResult<BaseIconVo> getInfo(@PathVariable("iconId") Long iconId) {
        return AjaxResult.success(iBaseIconService.queryById(iconId));
    }

    /**
     * 新增资源图标
     */
    @SaCheckPermission("base:icon:add")
    @Log(title = "资源图标", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BaseIconBo bo) {
        return toAjax(iBaseIconService.insertByBo(bo));
    }

    /**
     * 修改资源图标
     */
    @SaCheckPermission("base:icon:edit")
    @Log(title = "资源图标", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BaseIconBo bo) {
        return toAjax(iBaseIconService.updateByBo(bo));
    }

    /**
     * 删除资源图标
     */
    @SaCheckPermission("base:icon:remove")
    @Log(title = "资源图标" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{iconIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] iconIds) {
        return toAjax(iBaseIconService.deleteWithValidByIds(Arrays.asList(iconIds), true));
    }
}
